﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Data;
using DTO;
using UTIL;

namespace DAO
{
    public class NhaSanXuatDao
    {
        public static bool ThemNhaSanXuat(NhaSanXuatDto one)
        {
            bool result = false;
            try
            {
                // tao ket noi CSDL 
                SqlConnection cnn = DataConnector.MakeConnection();

                // goi proc thuc thi them loai nguoi dung
                SqlCommand cmd = StoreUtilities.CreateStoreCommand(cnn, "sp_themNhaSanXuat");
                StoreUtilities.AddInputPara(cmd, "@ten", SqlDbType.NVarChar, one.Ten);
                StoreUtilities.AddInputPara(cmd, "@diachi", SqlDbType.NVarChar, one.DiaChi);
                StoreUtilities.AddInputPara(cmd, "@dienthoai", SqlDbType.VarChar, one.DienThoai);

                int i = cmd.ExecuteNonQuery();
                if (i == 1)
                    result = true;

                // dong ket noi
                cnn.Close();
            }
            catch (Exception ex)
            {
                // bat loi 
                throw ex;

            }
            return result;
        }

        public static bool xoaNhaSanXuat(NhaSanXuatDto one)
        {
            bool result = false;
            try
            {
                // tao ket noi CSDL 
                SqlConnection cnn = DataConnector.MakeConnection();
                List<SanPhamDto> sanpham = SanPhamDao.LayDanhSachSanPhamTheoNhaSanXuat(one.ID);
                if (sanpham.Count != 0)
                {

                    for (int j = 0; j < sanpham.Count; j++)
                        SanPhamDao.xoaSanPham(sanpham[j].ID);

                }
                // goi proc thuc thi them loai nguoi dung
                SqlCommand cmd = StoreUtilities.CreateStoreCommand(cnn, "sp_xoaNhaSanXuat");
                StoreUtilities.AddInputPara(cmd, "@ID", SqlDbType.Int, one.ID);

                int i = cmd.ExecuteNonQuery();
                if (i == 1)
                    result = true;

                // dong ket noi
                cnn.Close();
            }
            catch (Exception ex)
            {
                // bat loi 
            }
            return result;
        }

        public static bool CapnhatNhaSanXuat(NhaSanXuatDto one)
        {
            bool result = false;
            try
            {
                // tao ket noi CSDL 
                SqlConnection cnn = DataConnector.MakeConnection();

                // goi proc thuc thi them loai nguoi dung
                SqlCommand cmd = StoreUtilities.CreateStoreCommand(cnn, "sp_capnhatNhaSanXuat");
                StoreUtilities.AddInputPara(cmd, "@ten", SqlDbType.NVarChar, one.Ten);
                StoreUtilities.AddInputPara(cmd, "@diachi", SqlDbType.NVarChar, one.DiaChi);
                StoreUtilities.AddInputPara(cmd, "@dienthoai", SqlDbType.VarChar, one.DienThoai);
                StoreUtilities.AddInputPara(cmd, "@ID", SqlDbType.Int, one.ID);

                int i = cmd.ExecuteNonQuery();
                if (i == 1)
                    result = true;

                // dong ket noi
                cnn.Close();
            }
            catch (Exception ex)
            {
                // bat loi 
                throw ex;

            }
            return result;
        }

        public static NhaSanXuatDto LayNhaSanXuat(int ID)
        {
            // ket noi CSDL
            SqlConnection cnn = DataConnector.MakeConnection();

            SqlCommand cmd = StoreUtilities.CreateStoreCommand(cnn, "sp_layNhaSanXuat");
            StoreUtilities.AddInputPara(cmd, "@ID", SqlDbType.Int, ID);

            SqlDataReader dr = cmd.ExecuteReader();
            NhaSanXuatDto nhasanxuat = null;
            while (dr.Read())
            {
                nhasanxuat = new NhaSanXuatDto();
                nhasanxuat.ID = (int)dr["ID"];
                nhasanxuat.Ten = (String)dr["Ten"];
                nhasanxuat.DiaChi = (String)dr["DiaChi"];
                nhasanxuat.DienThoai = (String)dr["DienThoai"];
            }
            // dong ket noi
            cnn.Close();

            return nhasanxuat;
        }

        public static List<NhaSanXuatDto> LayDanhSachNhaSanXuat()
        {
            // tao ket noi CSDL 
            SqlConnection cnn = DataConnector.MakeConnection();

            SqlCommand cmd = StoreUtilities.CreateStoreCommand(cnn, "sp_layDanhSachNhaSanXuat");

            SqlDataReader dr = cmd.ExecuteReader();

            List<NhaSanXuatDto> danhsach = new List<NhaSanXuatDto>();
            NhaSanXuatDto nhasanxuat = null;
            while (dr.Read())
            {
                nhasanxuat = new NhaSanXuatDto();
                nhasanxuat.ID = (int)dr["ID"];
                nhasanxuat.Ten = (String)dr["Ten"];
                nhasanxuat.DiaChi = (String)dr["DiaChi"];
                nhasanxuat.DienThoai = (String)dr["DienThoai"];

                danhsach.Add(nhasanxuat);
            }
            // dong ket noi
            cnn.Close();

            return danhsach;

        }

        public static List<NhaSanXuatDto> LayDanhSachTimKiemNhaSanXuat(String keyword)
        {
            // tao ket noi CSDL 
            SqlConnection cnn = DataConnector.MakeConnection();

            SqlCommand cmd = StoreUtilities.CreateStoreCommand(cnn, "sp_layDanhSachTimKiemNhaSanXuat");
            StoreUtilities.AddInputPara(cmd, "@keyword", SqlDbType.NVarChar, keyword);
            SqlDataReader dr = cmd.ExecuteReader();

            List<NhaSanXuatDto> danhsach = new List<NhaSanXuatDto>();
            NhaSanXuatDto nhasanxuat = null;
            while (dr.Read())
            {
                nhasanxuat = new NhaSanXuatDto();
                nhasanxuat.ID = (int)dr["ID"];
                nhasanxuat.Ten = (String)dr["Ten"];
                nhasanxuat.DiaChi = (String)dr["DiaChi"];
                nhasanxuat.DienThoai = (String)dr["DienThoai"];

                danhsach.Add(nhasanxuat);
            }
            // dong ket noi
            cnn.Close();

            return danhsach;

        }

        public static bool kiemtraTonTai(NhaSanXuatDto one)
        {
            bool result = false;
            NhaSanXuatDto temp = LayNhaSanXuat(one.ID);
            if (temp != null)
                result = true;

            return result;
        }

    }
}
